var lunboImgesUrl = "";
var imageUrlList = [[],[],[],[],[],[],[],[],[],[],[],[]];
var applyData;
var certifiedImgUrl;


init();

function init() {
    $("#exportPdf").hide();
    queryAuditList();
}

function savePic(){
    var formData = new FormData($( "#uploadPic" )[0]);
    console.log($("#uploadPic")[0]);
    $.ajax({
        type: "POST",
        url: prefixUrl + "image/upload",
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (data) {
            commodityThumbnail =  data;
            certifiedImgUrl = imageUrl + data;
            //$("#certifiedImgUrlSuc").attr("src",imageUrl + data);
        }
    });
}

function queryAuditList(pageIndex) {
    if (pageIndex == null) {
        pageIndex = 0;
    }
    var queryName = $("#queryName").val();
    var beginDate = $("#beginDate").val();
    var overDate = $("#overDate").val();
    $.ajax({
        url: prefixUrl + "apply/queryapply",
        type: "post",
        data: JSON.stringify({
            applyNo: queryName,
            applyName: queryName,
            beginDate: beginDate,
            overDate: overDate,
            pageIndex: pageIndex
        }),
        contentType: "application/json;",
        success: function (data) {
            goToLogin(data)
            var commonHtml = '<thead>'
                + '<tr>'
                + '<th>票据编号</th>'
                + '<th>票据名称</th>'
                + '<th>申请人</th>'
                + '<th>申请单位</th>'
                + '<th>申请时间</th>'
                + '<th>状态</th>'
                + '<th>操作</th>'
                + '</tr>'
                + '</thead>'
                + '<tbody>';
            var list = data.data.rows;
            for (var i = 0; i < list.length; i++) {
                if(list[i].auditState == null){
                    continue;
                }
                commonHtml = commonHtml
                    + '<tr>'
                    + '<td>'
                    + list[i].applyNo
                    + '</td>'
                    + '<td>'
                    + list[i].applyName
                    + '</td>'
                    + '<td>'
                    + list[i].applyPerson
                    + '</td>'
                    + '<td>'
                    + list[i].applyCompanyName
                    + '</td>'
                    + '<td>'
                    + new Date(list[i].oprDate).toLocaleDateString()
                    + '</td>';
                var state;
                if (list[i].auditState == 0) {
                    state = "未审查";
                    commonHtml = commonHtml
                        + '<td><span style="color: #1e252f">'
                        + state
                        + '<span/></td>'
                    commonHtml = commonHtml
                } else if (list[i].auditState == 1) {
                    state = "退回修改";
                    commonHtml = commonHtml
                        + '<td><span style="color: #ff9e3c">'
                        + state
                        + '<span/></td>'
                } else if (list[i].auditState == 2) {
                    state = "审查通过";
                    commonHtml = commonHtml
                        + '<td><span style="color: #12cd13">'
                        + state
                        + '<span/></td>'
                } else {
                    state = "审查不通过";
                    commonHtml = commonHtml
                        + '<td><span style="color: red">'
                        + state
                        + '<span/></td>'
                }
                commonHtml = commonHtml
                    + '<td>'
                    + '<button type="button" class="btn btn-info mar-r-5" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo" onclick=queryAuditDetail("'
                    + list[i].applyId
                    + '")><i class="fa fa-pencil"></i></button>'
                    + '<button type="button" class="btn btn-info mar-r-5" data-toggle="modal" data-whatever="@mdo" onclick=exportPdf("'
                    + list[i].applyId
                    + '","'
                    + list[i].certifiedImgUrl
                    + '") ><i class="fa fa-cloud-download"></i></button>'
                    + '</td>'
                    + '</tr>';
            }
            commonHtml = commonHtml + '</tbody>';
            $("#applyList").html(commonHtml);

            var total = data.data.total;
            var remainder = total % 20;
            var allNumber = 0;
            if (remainder > 0) {
                allNumber = parseInt(total / 20) + 1;
            } else {
                allNumber = parseInt(total / 20);
            }
            var number = 1;
            var numberHtml = '<li><a onclick=queryApplyList(' + "0" + ')>«</a></li>';
            for (var i = 0; i < allNumber; i++) {
                var id = "query-" + i;
                if (i == 0) {
                    numberHtml = numberHtml
                        + '<li id="' + id + '" name="queryList" class="active"><a onclick=queryAuditList(' + i + ')>' + number + '</a></li>';
                } else {
                    numberHtml = numberHtml
                        + '<li id="' + id + '" name="queryList"><a onclick=queryAuditList(' + i + ')>' + number + '</a></li>';
                }
                number++;
            }
            numberHtml = numberHtml + '<li><a onclick=queryAuditList(' + (allNumber - 1) + ')>»</a></li>';
            $("#queryList").html(numberHtml);
        }
    })
}


function exportPdf(applyId,imgUrl){
    $.ajax({
        url: prefixUrl + "audit/exportPdf",
        type: "post",
        data: JSON.stringify({
            applyId:applyId,
            certifiedImgUrl:imgUrl
        }),
        contentType: "application/json;",
        success: function (data) {
            if(data.code == 0){
                window.open(prefixUrl + "pdf/" + data.msg);
            }else{
                alert("导出失败，请联系管理人员");
            }
        }
    })
}


$("#queryName").change(function () {
    queryAuditList();
});
$("#beginDate").change(function () {
    queryAuditList();
});
$("#overDate").change(function () {
    queryAuditList();
});

function queryAuditDetail(id) {
    certifiedImgUrl = "";
    $.ajax({
        url: prefixUrl + "apply/qureysinple",
        type: "post",
        data: JSON.stringify({
            applyId: id
        }),
        contentType: "application/json;",
        success: function (data) {
            applyData = data.data;
            var detail = data.data;

            $("#applyNo").val(detail.applyNo);
            $("#applyName").val(detail.applyName);
            $("#applyCompanyName").val(detail.applyCompanyName);
            $("#applyType").val(detail.applyType);
            $("#applyPerson").val(detail.applyPerson);
            $("#applyAddr").val(detail.applyAddr);
            $("#applyPhone").val(detail.applyPhone);
            $("#applyStreet").val(detail.applyStreet);
            $("#auditDesc").val(detail.auditDesc);
            $("#image").attr("src",detail.certifiedImgUrl);
            /*if(detail.certifiedImgUrlSuc == null || detail.certifiedImgUrlSuc == ""){
                $("#certifiedImgUrlSuc").attr("src","images/z_add.png");
            }else{
                $("#certifiedImgUrlSuc").attr("src",detail.certifiedImgUrlSuc);
            }*/

            $("#applyNo").attr("disabled", "disabled");
            $("#applyName").attr("disabled", "disabled");
            $("#applyCompanyName").attr("disabled", "disabled");
            $("#applyType").attr("disabled", "disabled");
            $("#applyPerson").attr("disabled", "disabled");
            $("#applyAddr").attr("disabled", "disabled");
            $("#applyPhone").attr("disabled", "disabled");
            $("#applyStreet").attr("disabled", "disabled");

            if(detail.auditState == 2){
                $("#auditImg").attr("class","auditImgYes");
                $("#auditImg").show();
            }else if(detail.auditState == 1){
                $("#auditImg").attr("class","auditImgNo");
                $("#auditImg").show();
            }else{
                $("#auditImg").hide();
            }

            if (detail.auditState != 0) {
                $("#btn1").attr("disabled", "disabled");
                $("#btn2").attr("disabled", "disabled");
                $("#btn3").attr("disabled", "disabled");
            } else {
                $("#btn1").attr("disabled", false);
                $("#btn2").attr("disabled", false);
                $("#btn3").attr("disabled", false);
            }

            if(detail.auditState != 0){
                $("#uploadAuditImg").hide();
                $("#auditDesc").attr("disabled", "disabled");
            }else{
                $("#uploadAuditImg").show();
                $("#auditDesc").attr("disabled", false);
            }
            getImageUrlList(data.data.list);
            var detailList = getPageImageList(data.data.list);
            //轮播图回显
            if (detail.imgUrl != null && detail.imgUrl != "") {
                getLunBo(detailList);
            } else {
                getLunBo("");
            }
        }
    })
}

function updateState(auditState) {
    if(auditState == 1){//退回时
        if($("#auditDesc").val() == null || $("#auditDesc").val() == ""){
            //退回修改时，必须有审查意见
            alert("请输入审查意见");
            return;
        }
    }
    if($("#auditDesc").val().length > 50){
        alert("审核意见不可大于50个汉字");
        return;
    }
    var mymessage=confirm("您确定已审核完毕了么?");
    if(mymessage == false) {
        return;
    }
    var data = {
        applyId: applyData.applyId,
        applyNo: applyData.applyNo,
        auditState: auditState,
        auditDesc: $("#auditDesc").val(),
        certifiedImgUrl: certifiedImgUrl
    };
    $.ajax({
        url: prefixUrl + "audit/update_state",
        type: "post",
        data: JSON.stringify(data),
        contentType: "application/json;",
        success: function (data) {
            alert(data.msg);
            modalDisplay();
            location.href = "audit.html";
        }
    })
}

function getLunBo(lunboImgUrl) {
    lunboImgesUrl = lunboImgUrl;
}

function choiceImg(type) {
    window.imageUrlList = imageUrlList;
    window.addStatus = true;
    //window.open ('choiceImg.html?type=' + type, 'newwindow', 'height=800, width=1200, top=300, left=550, toolbar=no, menubar=no, resizable=no,location=no, status=no,titlebar=no,');
    window.open ('choiceImg.html?type=' + type);
}

function setAuditImgUrl(url){
    savePic();
}